import { PORT_RE, PORT_RANGE_RE } from '@util/constants';
import calcIntervalsCount from '@util/calc-intervals-count';

// 获取port中不重复port个数
const getPortsCount = ports => {
  const parsedPorts = [];

  ports.forEach(port => {
    if (PORT_RE.test(port)) {
      parsedPorts.push(Number(port));
    } else if (PORT_RANGE_RE.test(port)) {
      parsedPorts.push(port.split('-').map(Number));
    }
  });

  return calcIntervalsCount(parsedPorts);
};

export default getPortsCount;
